package com.seari.controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.dubbo.common.utils.StringUtils;
import com.github.pagehelper.util.StringUtil;
import com.seari.bean.DataGridBean;
import com.seari.pojo.FlowLinkDeploy;
import com.seari.pojo.FlowMajor;
import com.seari.pojo.RepairPlanData;
import com.seari.service.DeployDataService;
import com.seari.service.FlowLinkService;
import com.seari.utils.DateUtil;
import com.seari.utils.StringHelper;

/**
 * @author TOUC
 *
 */
@Controller
@RequestMapping("flowLink/")
public class FlowLinkController {

	@Autowired
	private DeployDataService deployDataService;
	@Autowired
	private FlowLinkService flowLinkService;

	/**
	 * @param req
	 * @return 添加故障处理流程
	 */
	@RequestMapping("addFaultFlowLink")
	@ResponseBody
	public DataGridBean<Object> addFaultFlowLink(HttpServletRequest req) {

		DataGridBean<Object> grid = new DataGridBean<Object>();

		// 故障id
		String faultId = req.getParameter("faultId");
		// 故障现象
		String faultContent = req.getParameter("faultContent");
		// 故障时间
		String faultTime = req.getParameter("faultTime");

		// 专业选择 9~13
		 String[] flowLinkIds = req.getParameterValues("flowLinkIds[]");
		//String[] flowLinkIds = { "11", "12" };
		if (StringUtils.isBlank(faultContent) || flowLinkIds.length < 1) {

			grid.setCode(StringHelper.MESSAGE_TYPE_ERROR);
			grid.setMsg("需选择专业及填写故障内容 !");
			return grid;

		} else {

			List<FlowMajor> fmList = new ArrayList<FlowMajor>();

			Integer flowLinkId = 0;

			for (int i = 0; i < flowLinkIds.length; i++) {
				String result = flowLinkService.queryFlowByfidAndFlid(faultId,flowLinkIds[i]);
				if (!StringUtil.isEmpty(result)) {
					grid.setCode(StringHelper.MESSAGE_TYPE_ERROR);
					grid.setMsg("请勿重复添加流程 !");
					return grid;
				}
				int fLId = Integer.parseInt(flowLinkIds[i]);
				List<FlowLinkDeploy> deployData = deployDataService
						.getDeployData(fLId);
				for (int j = 0; j < deployData.size(); j++) {
					FlowMajor fBranch = new FlowMajor();
					fBranch.setFaultId(faultId);
					fBranch.setFaultContent(faultContent);
					flowLinkId = deployData.get(j).getFlowLinkId();
					fBranch.setFlowLinkId(flowLinkId);
					fBranch.setAnswerTime(getTimeStr(flowLinkId, faultTime));
					fBranch.setTag(fLId);
					if (flowLinkId == 1) {

						fBranch.setStatu(1);
						fBranch.setDisposeTime(getTimeStr(flowLinkId, faultTime));

					} else if (flowLinkId == 2) {

						fBranch.setStatu(2);

					} else {

						fBranch.setStatu(0);

					}

					fmList.add(fBranch);

				}
			}

			int resualt = flowLinkService.addFlowLinkList(fmList);
			if (resualt > 0) {
				grid.setCode(StringHelper.MESSAGE_TYPE_SUCCESS);
				grid.setMsg("故障流程添加成功!");

			} else {
				grid.setCode(StringHelper.MESSAGE_TYPE_ERROR);
				grid.setMsg("故障流程添加失败!");
			}

		}

		return grid;

	}

	/**
	 * 分支流程编辑功能
	 * 
	 * @param req
	 * @return
	 */
	@RequestMapping("editMajor")
	@ResponseBody
	public DataGridBean<Object> editMajor(HttpServletRequest req) {

		DataGridBean<Object> grid = new DataGridBean<Object>();
		// 故障id
		String faultId = req.getParameter("faultId");
		// 专业选择 9~13
		String tag = req.getParameter("tag");
		// 故障时间
		String faultTime = req.getParameter("faultTime");
		// 添加OR删除 add delete
		String choice = req.getParameter("choice");

		if (choice.equals("delete")) {
			String result = flowLinkService.queryFlowByfidAndFlid(faultId,
					tag);
			if (StringUtil.isEmpty(result)) {
				grid.setCode(StringHelper.MESSAGE_TYPE_ERROR);
				grid.setMsg("无此流程 !");
			} else {

				Integer resualt = flowLinkService.deleteFlowByFlowLinkId(
						faultId, tag);

				if (resualt > 0) {
					// 删除专业时删除该专业的流程内容
					flowLinkService.deleteRepairPlanByFlowLinkId(faultId,
							tag, null);
					// 删除总流程中的该分支流程
					flowLinkService.deleteBranchFlowByFaultLinkId(faultId,
							tag);
					grid.setCode(StringHelper.MESSAGE_TYPE_SUCCESS);
					grid.setMsg("专业流程删除成功!");

				} else {
					grid.setCode(StringHelper.MESSAGE_TYPE_ERROR);
					grid.setMsg("专业流程删除失败!");
				}
			}

		} else if (choice.equals("add")) {

			String result = flowLinkService.queryFlowByfidAndFlid(faultId,
					tag);
			String faultContent = flowLinkService.queryFaultContent(faultId);
			if (StringUtil.isEmpty(result)) {
				List<FlowMajor> fmList = new ArrayList<FlowMajor>();
				// 分支流程ID
				int fLId = Integer.parseInt(tag);
				List<FlowLinkDeploy> deployData = deployDataService
						.getDeployData(fLId);
				// 总流程中添加分支流程
				/*
				 * FlowMajor fm = new FlowMajor(); fm.setFaultId(faultId);
				 * fm.setFaultContent(faultContent); fm.setFlowLinkId(fLId);
				 * fm.setAnswerTime(getTimeStr(fLId)); fm.setTag(1);
				 * fm.setStatu(0); int resu = flowLinkService.addFlowMajor(fm);
				 */

				// 流程线ID
				Integer fid = 0;
				for (int j = 0; j < deployData.size(); j++) {
					FlowMajor fBranch = new FlowMajor();
					fBranch.setFaultId(faultId);
					fBranch.setFaultContent(faultContent);
					fid = deployData.get(j).getFlowLinkId();
					fBranch.setFlowLinkId(fid);
					fBranch.setAnswerTime(getTimeStr(fid, faultTime));
					fBranch.setTag(fLId);
					if (fid == 1) {

						fBranch.setStatu(1);
						fBranch.setDisposeTime(getTimeStr(fid, faultTime));

					} else if (fid == 2) {

						fBranch.setStatu(2);

					} else {
						fBranch.setStatu(0);
					}

					fmList.add(fBranch);
				}
				int resualt = flowLinkService.addFlowLinkList(fmList);
				if (resualt > 0) {
					grid.setCode(StringHelper.MESSAGE_TYPE_SUCCESS);
					grid.setMsg("专业流程添加成功!");

				} else {
					grid.setCode(StringHelper.MESSAGE_TYPE_ERROR);
					grid.setMsg("专业流程添加失败!");
				}

			} else {
				grid.setCode(StringHelper.MESSAGE_TYPE_ERROR);
				grid.setMsg("当前流程已有此专业 !");

			}
		}

		return grid;
	}

	/**
	 * 记录流程内容
	 * 
	 * @param faultId
	 * @param flowLinkId
	 * @param tag
	 * @param projectText
	 * @return
	 */
	@RequestMapping("addRepairPlan")
	@ResponseBody
	public DataGridBean<Object> addRepairPlan(String faultId, String tag,
			String flowLinkId, String projectText) {

		DataGridBean<Object> grid = new DataGridBean<Object>();

		if (StringUtils.isBlank(faultId) || StringUtils.isBlank(tag)
				|| StringUtils.isBlank(flowLinkId)) {

			grid.setCode(StringHelper.MESSAGE_TYPE_ERROR);
			grid.setMsg("数据不能为空!");
			return grid;
		} else if (StringUtils.isBlank(projectText)) {

			// projectText输入为空时删除该流程内容
			flowLinkService.deleteRepairPlanByFlowLinkId(faultId, tag,
					flowLinkId);
			grid.setCode(StringHelper.MESSAGE_TYPE_SUCCESS);
			grid.setMsg("流程内容删除成功!");
			return grid;
		} else {

			// 查询该流程是否已经添加
			String text = flowLinkService.queryRepairPlan(faultId, tag,
					flowLinkId);

			// 为空则添加
			if (StringUtils.isBlank(text)) {

				RepairPlanData rpd = new RepairPlanData();
				rpd.setFaultId(faultId);
				rpd.setTag(Integer.parseInt(tag));
				rpd.setFlowLinkId(Integer.parseInt(flowLinkId));
				rpd.setTextContent(projectText);

				Integer result = flowLinkService.addRepairPlan(rpd);

				if (result > 0) {
					grid.setCode(StringHelper.MESSAGE_TYPE_SUCCESS);
					grid.setMsg("添加成功 !");
				} else {
					grid.setCode(StringHelper.MESSAGE_TYPE_ERROR);
					grid.setMsg("添加失败 !");
				}

			} else {
				// 不为空则更新内容
				RepairPlanData rpd = new RepairPlanData();
				rpd.setFaultId(faultId);
				rpd.setTag(Integer.parseInt(tag));
				rpd.setFlowLinkId(Integer.parseInt(flowLinkId));
				rpd.setTextContent(projectText);
				int result = flowLinkService.updateRepairPlan(rpd);
				if (result > 0) {
					grid.setCode(StringHelper.MESSAGE_TYPE_SUCCESS);
					grid.setMsg("更新成功 !");
				} else {
					grid.setCode(StringHelper.MESSAGE_TYPE_ERROR);
					grid.setMsg("更新失败 !");
				}

			}

		}

		return grid;
	}

	/**
	 * 查看抢救方案功能
	 * 
	 * @param faultId
	 * @param flowLinkId
	 * @param projectText
	 * @return
	 */
	@RequestMapping("queryRepairPlan")
	@ResponseBody
	public DataGridBean<Object> queryRepairPlan(String faultId, String tag,
			String flowLinkId) {

		DataGridBean<Object> grid = new DataGridBean<Object>();

		if (StringUtils.isBlank(faultId) || StringUtils.isBlank(tag)
				|| StringUtils.isBlank(flowLinkId)) {

			grid.setCode(StringHelper.MESSAGE_TYPE_ERROR);
			grid.setMsg("故障ID、专业ID或流程ID等不能为空!");

		} else {

			String rpd = flowLinkService.queryRepairPlan(faultId, tag,
					flowLinkId);

			if (StringUtils.isBlank(rpd)) {

				grid.setCode(StringHelper.MESSAGE_TYPE_ERROR);
				grid.setMsg("未查询到该流程数据信息!");

			} else {

				grid.setCode(StringHelper.MESSAGE_TYPE_SUCCESS);
				grid.setMsg(rpd);
			}

		}

		return grid;
	}

	private String getTimeStr(Integer flowLinkId, String faultTime) {
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat(
				"yyyyMMddHHmmss");
		Calendar nowTime = new GregorianCalendar();
		try {
			nowTime.setTime(simpleDateFormat.parse(faultTime));
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String timeStr = null;
		switch (flowLinkId) {
		case 1:
			nowTime.add(Calendar.MINUTE, 0);
			timeStr = sdf.format(nowTime.getTime());
			break;
		case 2:
			nowTime.add(Calendar.MINUTE, 5);
			timeStr = sdf.format(nowTime.getTime());
			break;
		case 3:
			nowTime.add(Calendar.MINUTE, 5);
			timeStr = sdf.format(nowTime.getTime());
			break;
		case 4:
			nowTime.add(Calendar.MINUTE, 7);
			timeStr = sdf.format(nowTime.getTime());
			break;
		case 5:
			nowTime.add(Calendar.MINUTE, 37);
			timeStr = sdf.format(nowTime.getTime());
			break;
		case 6:
			nowTime.add(Calendar.MINUTE, 42);
			timeStr = sdf.format(nowTime.getTime());
			break;
		case 7:
			nowTime.add(Calendar.MINUTE, 47);
			timeStr = sdf.format(nowTime.getTime());
			break;
		case 8:
			nowTime.add(Calendar.MINUTE, 52);
			timeStr = sdf.format(nowTime.getTime());
			break;
		case 9:
			nowTime.add(Calendar.MINUTE, 57);
			timeStr = sdf.format(nowTime.getTime());
			break;
		case 10:
			nowTime.add(Calendar.MINUTE, 57);
			timeStr = sdf.format(nowTime.getTime());
			break;
		case 11:
			nowTime.add(Calendar.MINUTE, 57);
			timeStr = sdf.format(nowTime.getTime());
			break;
		case 12:
			nowTime.add(Calendar.MINUTE, 57);
			timeStr = sdf.format(nowTime.getTime());
			break;
		case 13:
			nowTime.add(Calendar.MINUTE, 57);
			timeStr = sdf.format(nowTime.getTime());
			break;
		case 14:
			nowTime.add(Calendar.MINUTE, 57);
			timeStr = sdf.format(nowTime.getTime());
			break;
		case 15:
			nowTime.add(Calendar.MINUTE, 57);
			timeStr = sdf.format(nowTime.getTime());
			break;
		}
		return timeStr;
	}

}
